/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ihmprojet.view;

import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.TreeLayout;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ScalingControl;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import ihmprojet.core.Context;
import ihmprojet.core.model.Arrete;
import ihmprojet.core.model.DelegateTreeOperators;
import ihmprojet.core.model.Sommet;
import ihmprojet.view.renderer.EdgeLabelTransformer;
import ihmprojet.view.renderer.TreeOperatorRenderer;
import ihmprojet.view.transformer.IconLoader;
import ihmprojet.view.transformer.SommetIconTransformer;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.File;

/**
 *
 * @author thomas
 */
public class MiniMap extends javax.swing.JPanel {

    private Context context;
    private String id;
    private VisualizationViewer<Sommet, Arrete> vv;
    private FenetrePrincipale fenp;
    private ScalingControl scaler = new CrossoverScalingControl();// pour le zoom
    private int distx = DEFAULT_WIDTH;
    private int disty = DEFAULT_HEIGHT;
    private static final int DEFAULT_HEIGHT = 50;
    private static final int DEFAULT_WIDTH = 70;

    MiniMap(Context currentContext, String currentContextId, FenetrePrincipale fp) {
        initComponents();
        this.context = currentContext;
        this.id = currentContextId;
        this.setName(id);
        this.setLayout(new FlowLayout());
        renderTree();
        fenp = fp;
    }

    private Layout<Sommet, Arrete> createLayout() {
        Layout<Sommet, Arrete> layout = new TreeLayout<>(context.getTree(), distx, disty);
        return layout;
    }

    private void renderTree() {
        IconLoader il = new IconLoader();
        il.load(new File("images/tasks"), "gif");

        vv = new VisualizationViewer<>(this.createLayout());

        vv.setRenderer(new TreeOperatorRenderer<Sommet, Arrete>());
        DefaultModalGraphMouse dmgm = new DefaultModalGraphMouse();
        dmgm.setMode(ModalGraphMouse.Mode.PICKING);
        vv.setGraphMouse(dmgm);
        vv.getRenderContext().setVertexIconTransformer(new SommetIconTransformer(il));
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vv.getRenderContext().setEdgeLabelTransformer(new EdgeLabelTransformer());
        this.add(new GraphZoomScrollPane(vv));
        vv.setPreferredSize(new Dimension(170, 150));
        for (int i = 0; i < 10; i++) {
            zoomOut();
        }
    }

    void fireChangement() {
        vv.setGraphLayout(createLayout());
        this.repaint();
    }

    public void zoomIn() {
        setZoom(1);
    }

    public void zoomOut() {
        setZoom(-1);
    }

    private void setZoom(int amount) {
        scaler.scale(vv, amount > 0 ? 1.1f : 1 / 1.1f, vv.getCenter());
    }

    public String getId() {
        return id;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE));
    }// </editor-fold>                        
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}
